package top.mschen.blog.common.utils;

import org.assertj.core.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import top.mschen.blog.dto.MailBean;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 */
@Component
public class MailUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String  sender;

    /**
     * 发送邮件测试方法
     */
    public void sendMail(){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(sender);
        simpleMailMessage.setTo("1328256298@qq.com");
        simpleMailMessage.setSubject("SpringBoot集成JavaMail实现邮件发送");
        simpleMailMessage.setText("SpringBoot集成JavaMail实现邮件发送");
        mailSender.send(simpleMailMessage);
    }

    /**
     * 发送简单邮件
     * @param mailBean
     */
    public void sendMailSimple(MailBean mailBean){

        try {
            LOGGER.info( "邮件实体: {} ",mailBean);
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(sender);
            helper.setTo(sender);
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText());
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error( "发送简单邮件异常",e);
        }
    }

    /**
     * 发送邮件html
     * @param mailBean
     */
    public void sendMailHtml(MailBean mailBean){

        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(sender);
            helper.setTo(sender);
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(),true);
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            LOGGER.error( "发送邮件html",e);
        }
    }

    /**
     * 发送邮件带附件
     * @param mailBean
     */
    public void sendMailAttachment(MailBean mailBean){
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setFrom(sender);
            helper.setTo(sender);
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(),true);
            //添加附件
            helper.addAttachment(mailBean.getAttachmentFilename(), mailBean.getFile());
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            LOGGER.error( "发送邮件带附件",e);
        }
    }

    /**
     * 内联资源（静态资源）邮件发送
     * 由于邮件服务商不同，可能有些邮件并不支持内联资源的展示
     * 在测试过程中，新浪邮件不支持，QQ邮件支持
     * 不支持不意味着邮件发送不成功，而且内联资源在邮箱内无法正确加载
     * @param mailBean
     */
    public void sendMailInline(MailBean mailBean) {
         try {
             MimeMessage mimeMessage = mailSender.createMimeMessage();
             MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
             helper.setFrom(sender);
             helper.setTo(sender);
             helper.setSubject(mailBean.getSubject());
             /**
             * 内联资源邮件需要确保先设置邮件正文，再设置内联资源相关信息
             */
             helper.setText(mailBean.getText(), true);
             helper.addInline(mailBean.getContentId(), mailBean.getFile());
             mailSender.send(mimeMessage);
         } catch (MessagingException e) {
             LOGGER.error( "内联资源（静态资源）邮件发送",e);
         }
     }

    /**
     * 模板邮件发送
     * 默认是使用thymeleaf 默认位置
     * 根据约定大于配置，模板文件建议放在resources目录下。
     * 本例：/resources/templates/emailTemplate.html
     * @param mailBean
     */
    public void sendMailTemplate(MailBean mailBean) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sender);
            String[] array = Arrays.array("1328256298@qq.com", "1328256298@qq.com");
            helper.setTo(array);
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(), true);
            mailSender.send(mimeMessage);
        } catch (MessagingException e)  {
            LOGGER.error( "模板邮件发送",e);
        }
    }

}
